#include "calendar.h"

struct calendarStruct cal;

void Calendar_init(Calendar c)
{
	cal.day = c->day;
	cal.minute = c->minute;
	cal.second = c->second;
	cal.hour = c->hour;
	cal.month = c->month;
	cal.year = c->year;
	cal.flag = 0xff;
}

Calendar Calendar_getCalendar()
{
	return &cal;
}

bool Calendar_judge(Calendar c)
{
	if((c->year%4==0&&c->year%100!=0)||c->year%400==0)
	{
		if(c->month == 2 && c->day > 29)
		{
			return false;
		}
	}
	else
	{
		if(c->month == 2 && c->day > 28)
		{
			return false;
		}
	}
	if(c->month == 4 || c->month == 6 || c->month == 9 || c->month == 11)
	{
		if(c->day > 30)
		{
			return false;
		}
	}
	else
	{
		if(c->day > 31)
		{
			return false;
		}
	}
	if(c->second>=60 || c->minute>=60 || c->hour >=24 || c->month >= 13 || c->day ==0 || c->month ==0)
	{
		return false;
	}
	return true;
}

void Calendar_month_walk()
{
	cal.month++;
	if(cal.month >= 13)
	{
		cal.month = 1;
		cal.year++;
		cal.flag |= 1<<5;
	}
	cal.flag |= 1<<4;
}

void Calendar_day_walk()
{
	cal.day++;
	if(!Calendar_judge(&cal))
	{
		cal.day = 1;
		Calendar_month_walk();
	}
	cal.flag |= 1<<3;
}

void Calendar_hour_walk()
{
	cal.hour++;
	if(cal.hour >= 24)
	{
		cal.hour = 0;
		Calendar_day_walk();
	}
	cal.flag |= 1<<2;
}

void Calendar_minute_walk()
{
	cal.minute++;
	if(cal.minute >= 60)
	{
		cal.minute = 0;
		Calendar_hour_walk();
	}
	cal.flag |= 1<<1;
}

void Calendar_walk()
{
	cal.second++;
	if(cal.second >= 60)
	{
		cal.second = 0;
		Calendar_minute_walk();
	}
	cal.flag |= 1;
}
